<?php
$dbms = 'mysql';     //数据库类型
$host = 'db'; //数据库主机名，修改为容器名
$user = 'root';      //数据库连接用户名
$pass = $_ENV["MYSQL_ROOT_PASSWORD"]; //通过yml中定义的环境变量获得对应的密码
$dsn = "$dbms:host=$host;"; // "mysql:host=cdb"

try {
    $dbh = new PDO($dsn, $user, $pass); //初始化一个PDO对象
    echo "连接成功<br/>";
    $dbh = null;
} catch (PDOException $e) {
    die("Error!: " . $e->getMessage() . "<br/>");
}